package com.ttloner.springboot.controller;

import com.ttloner.springboot.model.MyUser;
import com.ttloner.springboot.service.MyUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author TTLoner
 * @date 2021/7/17 20:12
 * @description
 */
@Controller
public class MyUserController {
    @Autowired
    private MyUserService mus;

    @GetMapping("toinsert")
    public String toinsert() {
        return "toinsert";
    }
    @PostMapping("insert")
    public String insert(MyUser myUser) {
        mus.insert(myUser);
        return "redirect:main";
    }

    @GetMapping("selectById")
    public String selectById(int pid,Model model) {
        MyUser myUser = mus.selectById(pid);
        model.addAttribute("user",myUser);
        return "update";
    }
    @GetMapping("main")
    public String selectAll(Model model) {
        List<MyUser> list = mus.selectAll();
        model.addAttribute("userList",list);
        return "main";
    }

    @PostMapping("update")
    public String update(MyUser myUser) {
        mus.update(myUser);
        return "redirect:main";
    }

    @GetMapping("deleteById")
    public String deleteById(int pid) {
        mus.deleteById(pid);
        return "redirect:main";
    }
}
